home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / utils / datatypes / mpegsdt013.lha / classbase.c < prev    next >
C/C++ Source or Header  |  1998-02-05  |  15KB  |  468 lines

  1.  
  2. /*
  3. **
  4. **  $VER: classbase.c 1.3 (5.2.98)
  5. **  mpegsystem.datatype 1.3
  6. **
  7. **  Library routines for a DataTypes class
  8. **
  9. **  Written 1997/1998 by Roland 'Gizzy' Mainz
  10. **  Original example source from David N. Junod
  11. **
  12. */
  13.  
  14.  
  15. /* main includes */
  16. #include "classbase.h"
  17.  
  18.  
  19. /****** mpegsystem.datatype/MAIN *********************************************
  20. *
  21. *    INTRODUCTION
  22. *        Datatypes class for MPEG 1 System movies.
  23. *
  24. *    REQUIREMENTS
  25. *        - You need at least Kick/WB 3.0.
  26. *
  27. *        - datatypes.library >= V45
  28. *
  29. *        - "datatypes/animation.datatype", >= V40.
  30. *          "animation.datatype 40.6 (28.09.93)" requires itself some
  31. *          libraries/boopsi classes:
  32. *        - "realtime.library", >= V39              - for timing
  33. *        - "gadgets/tapedeck.gadget" (any version) - for the controls
  34. *
  35. *        - mpegvideo.datatype >= V1.6
  36. *
  37. *        - mpegaudio.datatype >= V1.1
  38. *
  39. *    USAGE
  40. *        If the datatypes descriptor file was activated, any attempt to load
  41. *        a MPEG System stream using GMultiView, MultiView, AmigaGuide or
  42. *        SwitchWindow will load and play the movie.
  43. *
  44. *    INSTALLATION
  45. *        After unpacking this archive:
  46. *        Because this version does not include an Installer script, you have
  47. *        to do the installation manually through the shell:
  48. *
  49. *          - Unpack this archive and copy the "mpegsystem.datatype" to
  50. *            SYS:Classes/DataTypes/:
  51. *
  52. *        Copy CLONE FROM "mpegsystem.datatype" TO
  53. *         "SYS:Classes/DataTypes/mpegsystem.datatype"
  54. *
  55. *          - Then copy the datatypes descriptor into the DEVS:DataTypes
  56. *            directory.
  57. *            If the descriptor already exists, you should not replace it,
  58. *            otherwise you may loose "toolnodes" and other settings stored in
  59. *            the existing descriptor.
  60. *
  61. *     Copy CLONE FROM "MPEG System(%|.info)" TO DEVS:Datatypes/
  62. *
  63. *    SOURCE
  64. *        Source is included as an example how to write a datatypes clas
  65. *        implements a virtual filesystem and embeds other datatype objects
  66. *        for it's work.
  67. *
  68. *    MODEL
  69. *        This datatype uses a very complex model for decoding MPEG system
  70. *        streams. The small drawing below shows the model (OK, the
  71. *        drawing is not very good, it's incomplete and so on. If someone
  72. *        has the time to draw a better one, send it to me).
  73. *
  74. *                           system stream
  75. *                                 |
  76. *                                 |
  77. *                                 Y
  78. *             +-------------------------------------------------------------+
  79. *             |            mpeg system datatype                             |
  80. *             +-----------------------------------------+-------------------+
  81. *             |              demultiplexer              | ADTM_LOADFRAME    |
  82. *             |  video 0...video n | audio 0... audio n | ADTM_UNLOADFRAME  |
  83. *             +-----+-------+------+------+-------+-----+-------------------+
  84. *                   |       |             |       |               ^ ^
  85. *                   |       |             |       |               | |
  86. *                   |       *             |       *               | |
  87. *                   |                     |                       | |
  88. *                   |                     |                       | |
  89. *                   Y                     Y                       | |
  90. *      +---------------------+   +---------------------+          | |
  91. *      | mpeg video datatype |   | mpeg audio datatype |          | |
  92. *      +-----------+---------+   +--------+------------+          | |
  93. *                  |                      |                       | |
  94. *                  |                      |                       | |
  95. *                  |                      +-----------------------+ |
  96. *                  +------------------------------------------------+
  97. *
  98. *    AUTHOR
  99. *        If you want to blame me, report any bugs, or wants a new version
  100. *        send your letter to:
  101. *                        Roland Mainz
  102. *                        Hohenstaufenstraße 8
  103. *                        52388 Nörvenich
  104. *                        GERMANY
  105. *
  106. *        Phone: (+49)(0)2426/901568
  107. *        Fax:   (+49)(0)2426/901569
  108. *
  109. *        EMAIL is also available (if you want to send me attachments
  110. *        larger than 1MB (up to 5MB, more with my permission):
  111. *
  112. *        GISBURN@w-specht.rhein-ruhr.de
  113. *
  114. *        Up to April 1998 I'm reachable using this email address, too:
  115. *        Reinhold.A.Mainz@KBV.DE
  116. *
  117. *        | Please put your name and address in your mails !
  118. *        | German mailers should add their phone numbers.
  119. *        | See BUGS section above when submitting bug reports.
  120. *
  121. *        Sorry, but I can only look once a week for mails.
  122. *        If you don't hear something from me within three weeks, please
  123. *        send your mail again (but watch about new releases) (problems with
  124. *        this email port are caused by reconfigurations, hackers, network
  125. *        problems etc.).
  126. *
  127. *        The  entire  "mpegsystem.datatype"  package  may  be  noncommercially
  128. *        redistributed, provided  that  the package  is always  distributed
  129. *        in it's complete  form (including it's documentation). A small
  130. *        copy fee  for media costs is okay but any kind of commercial
  131. *        distribution is strictly forbidden without my permission !
  132. *        Comments and suggestions how to improve this program are
  133. *        generally appreciated!
  134. *
  135. *        Thanks to David Junod, who wrote the animation.datatype and lots of
  136. *        the datatypes example code, Matt Dillon for his DICE,
  137. *        Olaf 'Olsen' Barthel for his help, ideas and some text clips from 
  138. *        his documentations.
  139. *
  140. ******************************************************************************
  141. *
  142. */
  143.  
  144.  
  145.  
  146. /****** mpegsystem.datatype/--datasheed-- ************************************
  147. *
  148. *   NAME
  149. *       mpegsystem.datatype -- data type for MPEG System streams
  150. *
  151. *   SUPERCLASS
  152. *       animation.datatype
  153. *
  154. *   DESCRIPTION
  155. *       The anim datatype, a sub-class of the animation.datatype, is used to
  156. *       load and play MPEG System movies.
  157. *
  158. *   METHODS
  159. *       OM_NEW -- Create a new animation object from a description file. The
  160. *           source may only be a file.
  161. *
  162. *       OM_DISPOSE -- Dispose instance and contents (embedded objects etc.
  163. *           etc.), then pass msg to superclass
  164. *
  165. *       OM_UPDATE -- Perform an ICM_CHECKLOOP check, and if succesfull, the
  166. *           method will be executed like OM_SET downstairs.
  167. *
  168. *       OM_SET -- Pass msg to superclass and call GM_RENDER if retval from
  169. *           superclass was != 0UL.
  170. *
  171. *       DTM_WRITE -- Save object's contents in local (MPEG System) or
  172. *           superclass (IFF ILBM) format.
  173. *           NOT IMPLEMENTED YET
  174. *
  175. *       ADTM_LOADFRAME -- Fill in struct adtFrame with requested information
  176. *           from embedded objects like bitmap, colormap and sample.
  177. *
  178. *       ADTM_UNLOADFRAME -- Free resources obtained by ADTM_UNLOADFRAME.
  179. *           The method is passed to the embedded objects.
  180. *
  181. *       All other methods are passed unchanged to superclass.
  182. *
  183. *   ATTRIBUTES
  184. *       Following attributes are set by the object and are READ-ONLY for
  185. *       applications:
  186. *
  187. *   BUGS
  188. *       - Support for streams with multiple video- and/or audio-streams
  189. *         has been removed. The datatype uses currently evertimes
  190. *         the first video- and the first audio-stream it finds.
  191. *
  192. *       - ADTM_START, ADT_PAUSE, ADTM_STOP and ADTM_LOCATE are not passed
  193. *         to the embedded mpevideo.datatype object(s), which may cause
  194. *         speed loss if this datatype does speed optimisations based on use
  195. *         of those methods.
  196. *
  197. *       - The sound may be out-of-sync in the case that a stream
  198. *         is very long (more than 60 minutes) due rounding problems.
  199. *         (In theory, I did not see the bug...).
  200. *
  201. *   TODO
  202. *       - Fixing the bugs above.
  203. *
  204. *       - Write the "--input_format--"-Autodoc section.
  205. *
  206. *       - Leading zeros in the beginning of the file except the "start code"
  207. *         begin sequence causes a stream not to be recognized by
  208. *         datatypes.library.
  209. *         May happen if someone reads raw data from a VideoCD.
  210. *
  211. *       - Writing the encoder part, using mpegvideo.datatype V2's encoder
  212. *         and mpegaudio.datatype's encoder parts.
  213. *
  214. *       - Implementation of ACTION_INFO in the internal filesystem that
  215. *         (in theory) optimized reading using async I/O gets correct
  216. *         parameters.
  217. *
  218. *       - Support for MPEG 2 system streams.
  219. *
  220. *       - QUICKSCAN option for optimized and much faster scanning of system
  221. *         streams.
  222. *
  223. *   HISTORY
  224. *       V1.1
  225. *         First public release.
  226. *
  227. *       V1.2
  228. *         - Fixed the descriptor (e.g. the file "MPEG System"). The
  229. *           old one contains unneccesary data in the comparisation mask.
  230. *           Now the descriptor matches the standard, except that
  231. *           leading zero bytes (0x00) causes that the stream won't be
  232. *           identified.
  233. *           Thanks to Steve Cutting (stevejc@c031aone.net.au) for notifyling
  234. *           me the bug.
  235. *           Fixed.
  236. *
  237. *         - Implemented IGNOREERRORS switch to get Steve Cutting's
  238. *           (stevejc@c031aone.net.au) example file
  239. *           "http://www.utexas.edustudents/cjso/Chabad/video/chase.mpg"
  240. *           working.
  241. *
  242. *         - Added missing VERBOSESYNTAX and DEBUG options to find problems
  243. *           in the datatype without recompiling the complete source.
  244. *
  245. *         - Fixed a bug in the FS:
  246. *           The internal filesystem now returns ERROR_SEEK_ERROR of someone
  247. *           send an ACTION_SEEK with an unknown position mode
  248. *           (e.g. if it is none of OFFSET_(BEGINNING|CURRENT|END).
  249. *           Fixed.
  250. *
  251. *         - Fixed some problems with the VERBOSE and DEBUG output (missing
  252. *           newlines, single lines > 75 chars etc.).
  253. *           Fixed.
  254. *
  255. *       V1.3
  256. *         - Minor code cleanup
  257. *
  258. *         - Fixed and updated the autodoc.
  259. *
  260. *         - Increased the handlers process priority up to 9
  261. *           (normal FFS tasks are running at priority 10) to get more speed
  262. *           during scanning.
  263. *
  264. *   SEE ALSO
  265. *       animation.datatype,
  266. *       anim.datatype,
  267. *       gifanim.datatype, mpegvideo.datatype,
  268. *       picmovie.datatype,
  269. *       cdxl.datatype, avi.datatype, quicktime.datatype,
  270. *       moviesetter.datatype,
  271. *       film.datatype,
  272. *       directory.datatype,
  273. *       markabletextdtclass
  274. *
  275. *******************************************************************************
  276. *
  277. */
  278.  
  279.  
  280. /****** mpegsystem.datatype/--input_format-- *********************************
  281. *
  282. *    NAME
  283. *        MPEG System -- MPEG System stream format
  284. *
  285. *    DESCRIPTION
  286. *        <Not written yet, sorry>
  287. *
  288. *    SEE ALSO
  289. *
  290. *******************************************************************************
  291. *
  292. */
  293.  
  294.  
  295.  
  296. /*****************************************************************************/
  297.  
  298. DISPATCHERFLAGS
  299. struct IClass *ObtainMPEGSystemEngine( REGA6 struct ClassBase *cb )
  300. {
  301.     return( (cb -> cb_Lib . cl_Class) );
  302. }
  303.  
  304. /*****************************************************************************/
  305.  
  306. DISPATCHERFLAGS
  307. struct Library *LibInit( REGD0 struct ClassBase *cb, REGA0 BPTR seglist, REGA6 struct ExecBase *sysbase )
  308. {
  309.     cb -> cb_SegList = seglist;
  310.     cb -> cb_SysBase = sysbase;
  311.  
  312.     InitSemaphore( (&(cb -> cb_Lock)) );
  313.  
  314.     /* Kickstart V3.0 ? */
  315.     if( (cb -> cb_SysBase -> LibNode . lib_Version) >= 39UL )
  316.     {
  317.       /* Obtain ROM libs */
  318.       if( cb -> cb_UtilityBase = OpenLibrary( "utility.library", 39UL ) )
  319.       {
  320.         if( cb -> cb_DOSBase = OpenLibrary( "dos.library", 39UL ) )
  321.         {
  322.           if( cb -> cb_GfxBase = OpenLibrary( "graphics.library", 39UL ) )
  323.           {
  324.             if( cb -> cb_IntuitionBase = OpenLibrary( "intuition.library", 39UL ) )
  325.             {
  326.               return( (&(cb -> cb_Lib . cl_Lib)) );
  327.  
  328. #ifdef COMMENTED_OUT
  329.               CloseLibrary( (cb -> cb_IntuitionBase) );
  330. #endif /* COMMENTED_OUT */
  331.             }
  332.  
  333.             CloseLibrary( (cb -> cb_GfxBase) );
  334.           }
  335.  
  336.           CloseLibrary( (cb -> cb_DOSBase) );
  337.         }
  338.  
  339.         CloseLibrary( (cb -> cb_UtilityBase) );
  340.       }
  341.     }
  342.  
  343.     return( NULL );
  344. }
  345.  
  346. /*****************************************************************************/
  347.  
  348. DISPATCHERFLAGS
  349. LONG LibOpen( REGA6 struct ClassBase *cb )
  350. {
  351.     LONG retval = (LONG)cb;
  352.     BOOL success = TRUE;
  353.  
  354.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  355.  
  356.     /* Use an internal use counter */
  357.     cb -> cb_Lib . cl_Lib . lib_OpenCnt++;
  358.     cb -> cb_Lib . cl_Lib . lib_Flags &= ~LIBF_DELEXP;
  359.  
  360.     if( (cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 1U )
  361.     {
  362.       if( (cb -> cb_Lib . cl_Class) == NULL )
  363.       {
  364.         success = FALSE;
  365.  
  366.         /* Only datatypes.library >= V45 supports virtual handlers... */
  367.         if( cb -> cb_DataTypesBase = OpenLibrary( "datatypes.library", 45UL ) )
  368.         {
  369.           /* Should require animation.datatype V41 */
  370.           if( cb -> cb_SuperClassBase = OpenLibrary( "datatypes/animation.datatype", 39UL /*41UL*/ ) )
  371.           {
  372.             if( cb -> cb_Lib . cl_Class = initClass( cb ) )
  373.             {
  374.               success = TRUE;
  375.             }
  376.           }
  377.         }
  378.       }
  379.     }
  380.  
  381.     if( !success )
  382.     {
  383.       CloseLibrary( (cb -> cb_SuperClassBase) );
  384.       CloseLibrary( (cb -> cb_DataTypesBase) );
  385.  
  386.       cb -> cb_DataTypesBase = cb -> cb_SuperClassBase = NULL;
  387.  
  388.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  389.  
  390.       retval = 0L;
  391.     }
  392.  
  393.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  394.  
  395.     return( retval );
  396. }
  397.  
  398. /*****************************************************************************/
  399.  
  400. DISPATCHERFLAGS
  401. LONG LibClose( REGA6 struct ClassBase *cb )
  402. {
  403.     LONG retval = 0L;
  404.  
  405.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  406.  
  407.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  408.     {
  409.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  410.     }
  411.  
  412.     if( ((cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 0U) && (cb -> cb_Lib . cl_Class) )
  413.     {
  414.       if( FreeClass( (cb -> cb_Lib . cl_Class) ) )
  415.       {
  416.         cb -> cb_Lib . cl_Class = NULL;
  417.  
  418.         CloseLibrary( (cb -> cb_SuperClassBase) );
  419.         CloseLibrary( (cb -> cb_DataTypesBase) );
  420.       }
  421.       else
  422.       {
  423.         cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  424.       }
  425.     }
  426.  
  427.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  428.  
  429.     if( (cb -> cb_Lib . cl_Lib . lib_Flags) & LIBF_DELEXP )
  430.     {
  431.       retval = LibExpunge( cb );
  432.     }
  433.  
  434.     return( retval );
  435. }
  436.  
  437. /*****************************************************************************/
  438.  
  439. DISPATCHERFLAGS
  440. LONG LibExpunge( REGA6 struct ClassBase *cb )
  441. {
  442.     BPTR seg;
  443.  
  444.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  445.     {
  446.       cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  447.  
  448.       seg = NULL;
  449.     }
  450.     else
  451.     {
  452.       Remove( (&(cb -> cb_Lib . cl_Lib . lib_Node)) );
  453.  
  454.       seg = cb -> cb_SegList;
  455.  
  456.       CloseLibrary( (cb -> cb_IntuitionBase) );
  457.       CloseLibrary( (cb -> cb_GfxBase) );
  458.       CloseLibrary( (cb -> cb_DOSBase) );
  459.       CloseLibrary( (cb -> cb_UtilityBase) );
  460.  
  461.       FreeMem( (APTR)((ULONG)(cb) - (ULONG)(cb -> cb_Lib . cl_Lib . lib_NegSize)), (ULONG)((cb -> cb_Lib . cl_Lib . lib_NegSize) + (cb -> cb_Lib . cl_Lib . lib_PosSize)) );
  462.     }
  463.  
  464.     return( (LONG)seg );
  465. }
  466.  
  467.  
  468.